☁️ cloud | June 10, 2021
NAT 인스턴스를 통하여 프라이빗 서브넷에 인스턴스가 외부 인터넷으로 통신을 가능하게 하는 실습
AWS 인프라 자원을 생성할 [YAML 파일]
Parameters:
KeyName:
Description: Name of an existing EC2 KeyPair to enable SSH access to the instances. Linked to AWS Parameter
Type: AWS::EC2::KeyPair::KeyName
ConstraintDescription: must be the name of an existing EC2 KeyPair.
LatestAmiId:
Description: (DO NOT CHANGE)
Type: 'AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>'
Default: '/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2'
AllowedValues:
- /aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2
Resources:
VPC1:
Type: AWS::EC2::VPC
Properties:
CidrBlock: 10.40.0.0/16
EnableDnsSupport: true
EnableDnsHostnames: true
Tags:
- Key: Name
Value: NATInstance-VPC1
InternetGateway1:
Type: AWS::EC2::InternetGateway
Properties:
Tags:
- Key: Name
Value: NATInstance-IGW1
InternetGatewayAttachment1:
Type: AWS::EC2::VPCGatewayAttachment
Properties:
InternetGatewayId: !Ref InternetGateway1
VpcId: !Ref VPC1
RouteTable1:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref VPC1
Tags:
- Key: Name
Value: NATInstance-PublicRouteTable1
DefaultRoute1:
Type: AWS::EC2::Route
DependsOn: InternetGatewayAttachment1
Properties:
RouteTableId: !Ref RouteTable1
DestinationCidrBlock: 0.0.0.0/0
GatewayId: !Ref InternetGateway1
Subnet1:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref VPC1
AvailabilityZone: !Select [0, !GetAZs '']
CidrBlock: 10.40.1.0/24
Tags:
- Key: Name
Value: NATInstance-VPC1-Subnet1
Subnet1RouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref RouteTable1
SubnetId: !Ref Subnet1
RouteTable2:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref VPC1
Tags:
- Key: Name
Value: NATInstance-PrivateRouteTable1
Subnet2:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref VPC1
AvailabilityZone: !Select [0, !GetAZs '']
CidrBlock: 10.40.2.0/24
Tags:
- Key: Name
Value: NATInstance-VPC1-Subnet2
Subnet2RouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref RouteTable2
SubnetId: !Ref Subnet2
Instance1ENIEth0:
Type: AWS::EC2::NetworkInterface
Properties:
SubnetId: !Ref Subnet1
Description: Instance1 eth0
GroupSet:
- !Ref SG1
PrivateIpAddress: 10.40.1.100
#SourceDestCheck: false
Tags:
- Key: Name
Value: NAT-Instance eth0
VPCEIP1:
Type: AWS::EC2::EIP
Properties:
Domain: vpc
VPCAssociateEIP1:
Type: AWS::EC2::EIPAssociation
Properties:
AllocationId: !GetAtt VPCEIP1.AllocationId
NetworkInterfaceId: !Ref Instance1ENIEth0
Instance1:
Type: AWS::EC2::Instance
Properties:
ImageId: !Ref LatestAmiId
InstanceType: t2.micro
KeyName: !Ref KeyName
Tags:
- Key: Name
Value: NAT-Instance
NetworkInterfaces:
- NetworkInterfaceId: !Ref Instance1ENIEth0
DeviceIndex: 0
UserData:
Fn::Base64: |
#!/bin/bash
hostname NAT-Instance
cat <<EOF>> /etc/sysctl.conf
net.ipv4.ip_forward=1
net.ipv4.conf.eth0.send_redirects=0
EOF
sysctl -p /etc/sysctl.conf
yum -y install iptables-services
systemctl start iptables && systemctl enable iptables
iptables -F
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
service iptables save
Instance2:
Type: AWS::EC2::Instance
DependsOn: Instance1
Properties:
ImageId: !Ref LatestAmiId
InstanceType: t2.micro
KeyName: !Ref KeyName
Tags:
- Key: Name
Value: Private-EC2-1
NetworkInterfaces:
- DeviceIndex: 0
SubnetId: !Ref Subnet2
GroupSet:
- !Ref SG2
PrivateIpAddress: 10.40.2.101
UserData:
Fn::Base64: |
#!/bin/bash
(
echo "qwe123"
echo "qwe123"
) | passwd --stdin root
sed -i "s/^PasswordAuthentication no/PasswordAuthentication yes/g" /etc/ssh/sshd_config
sed -i "s/^#PermitRootLogin yes/PermitRootLogin yes/g" /etc/ssh/sshd_config
service sshd restart
hostnamectl --static set-hostname Private-EC2-1
Instance3:
Type: AWS::EC2::Instance
DependsOn: Instance1
Properties:
ImageId: !Ref LatestAmiId
InstanceType: t2.micro
KeyName: !Ref KeyName
Tags:
- Key: Name
Value: Private-EC2-2
NetworkInterfaces:
- DeviceIndex: 0
SubnetId: !Ref Subnet2
GroupSet:
- !Ref SG2
PrivateIpAddress: 10.40.2.102
UserData:
Fn::Base64: |
#!/bin/bash
(
echo "qwe123"
echo "qwe123"
) | passwd --stdin root
sed -i "s/^PasswordAuthentication no/PasswordAuthentication yes/g" /etc/ssh/sshd_config
sed -i "s/^#PermitRootLogin yes/PermitRootLogin yes/g" /etc/ssh/sshd_config
service sshd restart
hostnamectl --static set-hostname Private-EC2-2
SG1:
Type: AWS::EC2::SecurityGroup
Properties:
VpcId: !Ref VPC1
GroupDescription: VPC1-NATInstance-SecurityGroup
Tags:
- Key: Name
Value: VPC1-NATInstance-SecurityGroup
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: '22'
ToPort: '22'
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: '80'
ToPort: '80'
CidrIp: 10.40.0.0/16
- IpProtocol: tcp
FromPort: '443'
ToPort: '443'
CidrIp: 10.40.0.0/16
- IpProtocol: udp
FromPort: '0'
ToPort: '65535'
CidrIp: 10.40.0.0/16
- IpProtocol: icmp
FromPort: -1
ToPort: -1
CidrIp: 0.0.0.0/0
SG2:
Type: AWS::EC2::SecurityGroup
Properties:
VpcId: !Ref VPC1
GroupDescription: VPC1-PrivateEC2-SecurityGroup
Tags:
- Key: Name
Value: VPC1-PrivateEC2-SecurityGroup
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: '22'
ToPort: '22'
CidrIp: 10.40.0.0/0
- IpProtocol: icmp
FromPort: -1
ToPort: -1
CidrIp: 0.0.0.0/0
기본적으로 프라이빗 EC2 인스턴스 2개는 외부에서 접근이 불가능합니다.
프라이빗 EC2 인스턴스 정보 확인
IPv4 라우팅 처리
와 IP masquerade
동작을 확인IP masquerade: 리눅스에서 지원하는 네트워킹의 한 기능
- 내부 인스턴스의 IP와 포트를 NAT 인스턴스의 IP와 포트로 변환(PAT)
기본적으로 인스턴스로 인입되거나 나가는 트래픽이(자신의 - 출발지IP, 목적지IP)가 아닐 경우 폐기합니다.
ICMP[Internet Control Message Protocol]: 인터넷 제어 메시지 프로토콜
용도
- 오류 메시지를 전송받는 데 주로 이용
- ICMP 프로토콜은 Network 계층에 속하며 IP 프로토콜과 같이 사용한다!
사용 명령어
- Ping 명령어: 상대방 호스트의 작동 여부 및 응답시간 측정
- Tracert 명령어: 목적지까지의 라우팅 경로 추적을 하기 위해 사용
tcpdump
명령어를 통해 NAT-Instance에서 경유하는지 확인